<?php

/**
 * This is the model class for table "acticles".
 *
 * The followings are the available columns in table 'acticles':
 * @property integer $id
 * @property string $name
 * @property string $alias
 * @property string $description
 * @property string $content
 * @property integer $acricle_category_id
 * @property string $images
 * @property integer $created
 */
class Acticles extends PIActiveRecord
{
	public $image;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Acticles the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'acticles';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, alias, content, acticle_category_id, created', 'required','message'=>'{attribute} không được trống'),
			array('acticle_category_id, created', 'numerical', 'integerOnly'=>true),
			array('name, alias, image', 'length', 'max'=>255),
			array('description', 'length', 'max'=>1024),
			array('image', 'file', 'types'=>'jpg, gif, png', 'allowEmpty' => true,'on' => 'create'),
			array('image', 'file', 'types'=>'jpg, gif, png', 'allowEmpty' => true ,'on' => 'update'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, alias, description, content, acticle_category_id, image, created', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'ActicleCategories'=>array(self::BELONGS_TO, 'ActicleCategories', 'acticle_category_id')
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Tên bài viết',
			'alias' => 'Alias',
			'description' => 'Mô tả ngắn',
			'content' => 'Nội dung bài viết',
			'acticle_category_id' => 'Danh mục bài viêt',
			'image' => 'Hình ảnh',
			'created' => 'Ngày tạo',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('alias',$this->alias,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('acricle_category_id',$this->acricle_category_id);
		$criteria->compare('images',$this->images,true);
		$criteria->compare('created',$this->created);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function getImage()
	{
		$image = $this->image;
		$parentCategory = $this->ActicleCategories->parent;
		if($parentCategory == 5){
			if(empty($image))
				return 'units.png';
			return $image;
		}else{
			if(empty($image))
				return 'no-image.png';
			return $image;
		}
	}
	public function getLink($full = false){
		if($full == true){
			return PIUrl::createAbsoluteUrl('/acticle/detail',array('alias'=>$this->alias));
		}else{
			return PIUrl::createUrl('/acticle/detail',array('alias'=>$this->alias));
		}
	}
	public function getUnit()
	{
		$title = $this->name;
		$unit = explode(':',$title);
		if(count($unit) > 1)
			return $unit[0];
		else
			return '';
	}
	public function getActicleSoCap($limit=5){
		$criteria = new CDbCriteria();
		$criteria->condition = "acticle_category_id=:id";
		$criteria->params = array(':id'=>10);
		$criteria->limit = $limit;
		$criteria->order = "id DESC";
		return self::model()->findAll($criteria);
	}
	public function getActicleTrungCap($limit=5){
		$criteria = new CDbCriteria();
		$criteria->condition = "acticle_category_id=:id";
		$criteria->params = array(':id'=>11);
		$criteria->limit = $limit;
		$criteria->order = "id DESC";
		return self::model()->findAll($criteria);
	}
	public function getActicleCaoCap($limit=5){
		$criteria = new CDbCriteria();
		$criteria->condition = "acticle_category_id=:id";
		$criteria->params = array(':id'=>12);
		$criteria->limit = $limit;
		$criteria->order = "id DESC";
		return self::model()->findAll($criteria);
	}
	public function getActicleThuVien($limit=5){
		$criteria = new CDbCriteria();
		$criteria->condition = "acticle_category_id=:id";
		$criteria->params = array(':id'=>3);
		$criteria->limit = $limit;
		$criteria->order = "id DESC";
		return self::model()->findAll($criteria);
	}
}